﻿using System;

namespace ThresholdAssistant.Enums.Extensions
{
    /// <summary>
    /// 设备类型扩展信息
    /// </summary>
    public static class DeviceExtensions
    {
        /// <summary>
        /// 转换为长的设备编号
        /// </summary>
        /// <param name="deviceNo">旧的设备编号</param>
        /// <param name="protocolType">协议类型</param>
        /// <param name="robotType">机器人类型(平台号,扫码号,托管号)</param>
        /// <returns></returns>
        public static string ToDeviceHeartNo(this string deviceNo, RobotProtocolType protocolType, RobotTypeEnum robotType)
        {
            if (string.IsNullOrWhiteSpace(deviceNo))
                return "";

            var deviceType = protocolType.ToDeviceType(robotType);
            return $"{deviceNo}{deviceType.GetHashCode().ToString().PadLeft(2, '0')}";
        }

        /// <summary>
        /// 转换设备类型
        /// </summary>
        /// <param name="protocolType">协议类型</param>
        /// <param name="robotType">机器人类型</param>
        /// <returns></returns>
        public static DeviceTypeEnum ToDeviceType(this RobotProtocolType protocolType, RobotTypeEnum robotType)
        {
            DeviceTypeEnum? deviceType = null;
            switch (protocolType)
            {
                case RobotProtocolType.iPad:
                case RobotProtocolType.iPhone:
                    deviceType = (robotType == RobotTypeEnum.ScanCode) ?
                        DeviceTypeEnum.ScanCode : DeviceTypeEnum.IOS;
                    break;
                case RobotProtocolType.AndroidSimulator:
                case RobotProtocolType.android_box:
                case RobotProtocolType.android_phone:
                    deviceType = DeviceTypeEnum.AndroidSimulator;
                    break;
                case RobotProtocolType.Android:
                    deviceType = DeviceTypeEnum.Android;
                    break;
                case RobotProtocolType.windows:
                    deviceType = DeviceTypeEnum.Windows;
                    break;
                case RobotProtocolType.windows_guard:
                    deviceType = DeviceTypeEnum.ChatRoomGuard;
                    break;
                case RobotProtocolType.windows_protocol:
                    deviceType = DeviceTypeEnum.WindowsProtocol;
                    break;
                case RobotProtocolType.az_protocol:
                    deviceType = DeviceTypeEnum.az_protocol;
                    break;
                case RobotProtocolType.windows_pdd:
                    deviceType = DeviceTypeEnum.windows_pdd;
                    break;
                case RobotProtocolType.mac_protocol:
                    deviceType = DeviceTypeEnum.mac_protocol;
                    break;
                case RobotProtocolType.android_pad_protocol:
                    deviceType = DeviceTypeEnum.android_pad_protocol;
                    break;
                case RobotProtocolType.cloud_protocol:
                    deviceType = DeviceTypeEnum.cloud_protocol;
                    break;
                case RobotProtocolType.windows_local:
                    deviceType = DeviceTypeEnum.windows_local;
                    break;
            }

            if (!deviceType.HasValue)
            {
                throw new ArgumentException("PushTargetType参数protocolType异常");
            }
            return deviceType.Value;
        }
    }
}